/*
 * VideoServiceImpl.java
 * Copyright 2018 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

package com.bearox.trans.service.impl;

import com.bearox.trans.data.domain.Video;
import com.bearox.trans.data.dto.VideoDto;
import com.bearox.trans.mapper.VideoMapper;
import com.bearox.trans.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bearox
 */
@Service
public class VideoServiceImpl implements VideoService {

    private final VideoMapper videoMapper;

    public VideoServiceImpl(final VideoMapper videoMapper) {
        this.videoMapper = videoMapper;
    }

    @Override
    public int addVideo(final VideoDto videoDto) {
        if (!checkVideo(videoDto)) {
            return 0;
        }
        final Video video = new Video();
        BeanUtils.copyProperties(videoDto, video);
        return videoMapper.add(video);
    }

    @Override
    public List<VideoDto> getVideoListByCourseId(final long coureId, final long start,
            final int num) {
        final List<Video> videos = videoMapper.getVideoByCourseId(coureId, start, num);
        return CollectionUtils.isEmpty(videos) ? Collections.emptyList() :
                videos.stream().map(a -> {
                    final VideoDto videoDto = new VideoDto();
                    BeanUtils.copyProperties(a, videoDto);
                    return videoDto;
                }).collect(Collectors.toList());
    }

    private boolean checkVideo(final VideoDto videoDto) {
        return videoDto != null && videoDto.getAuthorId() != null && videoDto.getCourseId() != null
                && videoDto.getVideoUrl() != null;
    }
}
